<?php

namespace App\Admin\Controllers\Goods;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Models\Goods\CmcGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoodsCate;
use Dcat\Admin\Widgets\Dropdown;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class CmcGoodsController extends AdminController
{

    public function __construct(Request $request)
    {
        $this->goods_is_not = $request->goods_is_not;
        return $this;
    }

    public function grid()
    {

        return Grid::make(new CmcGood(), function (Grid $grid) {
            $grid->withBorder();
            $grid->tools('<a class="btn btn-primary disable-outline">测试按钮</a>');
//            $grid->tools(new GoodsGender());
              $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(CmcGood::class));
                }
            });

            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(CmcGood::class));
                }
            });
            if ($this->goods_is_not == 0) {
                $grid->model()->where('goods_is_not',0);
            } elseif ($this->goods_is_not == 1) {
                $grid->model()->where("goods_is_not",1);
            } else {
                $grid->model()->where("goods_is_not",2);
            }

            $grid->header(function () {
                $tab = Tab::make();
                $tab->addLink('出售的产品', '?goods_is_not=0',$this->goods_is_not==0 ? true : false);
                $tab->addLink('仓库中的商品', '?goods_is_not=1',$this->goods_is_not==1 ? true : false);
                $tab->addLink('已售商品', '?goods_is_not=2',$this->goods_is_not==2 ? true : false);
                return $tab;
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->group('price', function ($group) {
                    $group->gt('大于');
                    $group->lt('小于');
                    $group->nlt('不小于');
                    $group->ngt('不大于');
                    $group->equal('等于');
                });
                $filter->equal('goods_attr_class')->select(['1'=>'每日新品','2'=>'热门推荐','3'=>'家用电器','4'=>'美妆个护']);
            });

            $grid->column('goods_id')->sortable();
            $grid->column('goods_image')->image($server = null, $width=100, $height=120);
            $grid->column('goods_name')->editable(true);
            $grid->column('price')->editable(true);;
            $grid->column('goods_stock');
            $grid->column('sale_num');
            $grid->column('sort');
            $grid->column('goods_state', '状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    1=> '上架',
                    0 => '下架',
                ])
            );
            $grid->column('create_time');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('goods_id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcGood(), function (Show $show) {
            $show->field('goods_id');
            $show->field('goods_name');
            $show->field('goods_keyname');
            $show->field('goods_class');
            $show->field('goods_class_name');
            $show->field('goods_attr_class');
            $show->field('goods_attr_name');
            $show->field('goods_image');
            $show->field('goods_content');
            $show->field('goods_state');
            $show->field('category_id');
            $show->field('price');
            $show->field('market_price');
            $show->field('cost_price');
            $show->field('goods_stock');
            $show->field('goods_stock_alarm');
            $show->field('is_virtual');
            $show->field('is_free_shipping');
            $show->field('shipping_template');
            $show->field('goods_spec_format');
            $show->field('goods_attr_format');
            $show->field('is_delete');
            $show->field('introduction');
            $show->field('keywords');
            $show->field('sort');
            $show->field('create_time');
            $show->field('modify_time');
            $show->field('video_url');
            $show->field('sale_num');
            $show->field('evaluate');
            $show->field('evaluate_shaitu');
            $show->field('evaluate_shipin');
            $show->field('evaluate_zhuiping');
            $show->field('evaluate_haoping');
            $show->field('evaluate_zhongping');
            $show->field('evaluate_chaping');
            $show->field('is_consume_discount');
            $show->field('discount_config');
            $show->field('discount_method');
            $show->field('promotion_addon');
            $show->field('label_id');
            //$show->field('virtual_sale');
            $show->field('max_buy');
            $show->field('min_buy');
            $show->field('created_at');
            $show->field('updated_at');

        });
    }

    /**
     * Notes:
     * User: w2011344@qq.com
     * Date: 2021/7/18
     * Time: 21:28
     * @return Form
     */
    protected function form()
    {
        return Form::make(new  CmcGood(), function (Form $form) {
            $form->title('商品信息');
            // 设置字段宽度
            $form->width(9, 2);

            $form->text('goods_name');
            $form->text('keywords');
            $form->text('introduction');
            $form->image('goods_albums');
            $form->select('is_free_shipping')->options(
                [
                    0 => '不包邮',
                    1 => '包邮',
                ]
            );

            $form->select('goods_class_name')->options(
                CmcGoodsCate::table('cmc_goods_category')->pluck('category_name')
            );
            $form->image('goods_image');

            $form->editor('goods_content');
            $form->number('sort');
            $form->number('goods_integral');
            $form->number('virtual_sale');

                        $form->radio('goods_attr_format', '规格')
                ->options([
                    1 => '单规格',
                    2 => '多规格',
                ])->when(1, function (Form $form) {

                                $form->number('price');
                                $form->number('cost_price');
                                $form->number('price');
                                $form->text('goods_numbering');
                                $form->number('goods_stock');

                })->when(2, function (Form $form) {

                                $form->number('price');
                                $form->number('cost_price');
                                $form->select('discount_config')
                                    ->options(
                                        [
                                            0=>'颜色',
                                            1 =>'耗电量',
                                            2 =>'颜色',
                                        ]
                                    );

                                $form->number('goods_stock');
                                $form->text('goods_numbering');
                });
                        // 设置标题

        });

    }
}
//